import 'package:flutter/material.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:sidebarx/sidebarx.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:get/get.dart';

String currentSelectedPageRoute = '/individual/audiobooks';

class IndividualSidebarX extends StatefulWidget {
  const IndividualSidebarX(
      {Key? key, required this.selectedIndex, required this.isBigScreen})
      : super(key: key);
  final int selectedIndex;
  final bool isBigScreen;

  @override
  State<IndividualSidebarX> createState() => _IndividualSidebarXState();
}

class _IndividualSidebarXState extends State<IndividualSidebarX> {
  final SidebarXController controller =
      SidebarXController(selectedIndex: 0, extended: false);
  final PageStyles _pageStyles = PageStyles();

  @override
  void initState() {
    super.initState();
    controller.selectIndex(widget.selectedIndex);
    if (widget.isBigScreen) {
      controller.setExtended(true);
    }
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('start building IndividualSidebarX');

    return Container(
      margin: _pageStyles.sideBarPageEdgeInsets(),
      decoration: _pageStyles.pageSidebarDecoration(),
      child: Column(
        children: [
          Expanded(
            flex: 2,
            child: SidebarX(
              controller: controller,
              theme: _pageStyles.pageSidebarXTheme(),
              extendedTheme: _pageStyles.pageSidebarXExtendTheme(),
              headerBuilder: (context, extended) {
                return Container(
                  //height: 120,
                  child: Container(
                    margin: EdgeInsets.symmetric(vertical: 3.0),
                    padding: EdgeInsets.all(3.0),
                    decoration: BoxDecoration(
                        color: Get.isDarkMode
                            ? Colors.white.withOpacity(0.1)
                            : Colors.black.withOpacity(0.1),
                        shape: BoxShape.circle),
                    child: GestureDetector(
                        onTap: () {
                          Get.offAllNamed('/individual/home');
                          // if (GetPlatform.isWeb) {
                          //   Get.offAllNamed('/individual/home');
                          // } else {
                          //   Get.offAllNamed('/individual/home');
                          // }
                        },
                        child: Image.asset('assets/images/brand.png')),
                  ),
                );
              },
              //lists 之间没有调整间距的选项
              items: RoutePages.individualPageItems
                  .map((page) => SidebarXItem(
                        iconWidget: Tooltip(
                          textStyle: TextStyle(
                              color: Get.isDarkMode
                                  ? Get.theme.primaryColor
                                  : Get.theme.primaryColorLight),
                          message: page.name.tr,
                          child: Icon(page.icon,
                              color: Get.isDarkMode
                                  ? Get.theme.primaryColorDark
                                  : Get.theme.primaryColorLight),
                        ),
                        //icon: page.icon,
                        label: page.name.tr,
                        onTap: () {
                          if (page.name == 'Home') {
                            Get.offAllNamed('/individual/home');
                          } else {
                            if (GetPlatform.isWeb) {
                              if (currentSelectedPageRoute ==
                                  '/individual/audiobooks') {
                                currentSelectedPageRoute = page.route;
                                Get.offAllNamed(page.route);
                              } else {
                                currentSelectedPageRoute = page.route;
                                Get.toNamed(page.route);
                              }
                            } else {
                              Get.offAllNamed(page.route);
                            }
                          }
                        },
                      ))
                  .toList(),
            ),
          ),
        ],
      ),
    );
  }
}
